#!/bin/sh
. /etc/utils.sh

case "$1" in
  start)
    mkdir -p /tmp/wifi/fingerprints
    mkdir -p /tmp/wifi/wifiblaster
    mkdir -p /tmp/wifi/wifiinfo
    mkdir -p /tmp/stations
    if runnable wl && has_wifi && interface_exists eth2; then
      read wifimac </sys/class/net/eth2/address
      wl ap 1
      wl radio off
      # Q2 == US with no DFS channels
      wl country Q2
      wl bssid "$wifimac"
    fi
    if runnable wifi_files; then
      babysit 30 wifi_files 2>&1 | logos wifistatus &
    fi
    if interface_exists quantenna; then
      babysit 30 monitor-bbic-temp 2>&1 | logos bbictemp &
      babysit 30 quantenna-kmsgd 2>&1 | logos quantenna-kmsgd &
      babysit 30 quantenna-status 2>&1 | logos quantenna-status &
    fi
    iw reg set US
    wifi restore
    exit 0
    ;;
  stop)
    wifi stop
    pkillwait -f wifi_files
    pkillwait -f monitor-bbic-temp
    pkillwait -f quantenna-kmsgd
    pkillwait -f quantenna-status
    rm -rf /tmp/stations
    rm -rf /tmp/wifisignal
    ;;
  restart)
    $0 stop; $0 start
    ;;
  *)
    echo "Usage: $0 {start|stop|restart}"
    exit 1
esac
